//堆排序
#include <iostream>
using namespace std;
const int N = 1e6 + 10;

int h[N] , size , n;

void down(int u)
{
    int t = u;
    if(u*2 <= size && h[u*2] < h[t]) t = u*2;
    if(u*2+1 <= size && h[u*2+1] < h[t]) t = u*2+1;
    if(t != u)
    {
        swap(h[t],h[u]);
        down(t);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i = 1 ; i <= n ; ++i) scanf("%d",&h[i]);
    size = n;
    //建堆
    for(int i = n/2 ; i ; --i) down(i);
    
    //堆排序
    for(int i = 1 ; i <= n ; ++i)
    {
        printf("%d ",h[1]);
        h[1] = h[size--];
        down(1);
    }
    return 0;
}